package metier.perso;

import java.awt.Image;
import java.io.File;

import javax.imageio.ImageIO;

import controleur.Controleur;

public abstract class Perso implements Runnable{
	
	private Controleur cont;
	private Image[] images;
	private int imageEnCours;
	private double posX;
	private double posY;
	private String nom;
	private int pvMax;
	private int pv;
	private int manaMax;
	private int mana;
	private int level;
	private Thread thread;
	boolean enDeplacement;
	
	public Perso(Controleur cont, String[] addrImages, int imagesEnCours, int posX, int posY,
			String nom, int pvMax, int pv, int manaMax, int mana, int level) {
		super();
		this.imageEnCours = imagesEnCours;
		this.posX = posX;
		this.posY = posY;
		this.nom = nom;
		this.pvMax = pvMax;
		this.pv = pv;
		this.level = level;
		this.manaMax = manaMax;
		this.mana = mana;
		this.cont = cont;
		this.enDeplacement = false;
		
		images = new Image[addrImages.length];
		for(int i = 0; i < images.length; i++) {
			try {
				images[i] = ImageIO.read(new File("images/"+addrImages[i]));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public int getManaMax() {
		return manaMax;
	}

	public int getMana() {
		return mana;
	}

	public void setManaMax(int manaMax) {
		this.manaMax = manaMax;
	}

	public void setMana(int mana) {
		this.mana = mana;
	}

	public Image getImageEnCours() {
		return images[imageEnCours];
	}

	public double getPosX() {
		return posX;
	}

	public double getPosY() {
		return posY;
	}

	public String getNom() {
		return nom;
	}

	public int getPvMax() {
		return pvMax;
	}

	public int getPv() {
		return pv;
	}

	public int getLevel() {
		return level;
	}

	public void setImageEnCours(int imageEnCours) {
		this.imageEnCours = imageEnCours;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}

	public void setPosY(int posY) {
		this.posY = posY;
	}

	public void setPvMax(int pvMax) {
		this.pvMax = pvMax;
	}

	public void setPv(int pv) {
		this.pv = pv;
	}
	
	public void setLevel(int level) {
		this.level = level;
	}
	
	public void deplacer(char dir) {
		if(!enDeplacement) {
			thread = new Thread(this);
			thread.start();
			double posX = this.posX;
			double posY = this.posY;
			
			switch(dir) {
			case 'n' : posY--; break;
			case 's' : posY++; break;
			case 'e' : posX++; break;
			case 'o' : posX--; break;
			}
			
			if(cont.getMapEnCours().getCase((int)posX, (int)posY).parcourable()) {
				this.posX = posX;
				this.posY = posY;
			}
		}
	}
	
	public void run() {
		enDeplacement = true;
		try {
			Thread.sleep(150);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		enDeplacement = false;
	}
}
